package cn.iocoder.yudao.module.train.controller.wechat.appointment;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.train.controller.admin.appointment.vo.AppointmentPageReqVO;
import cn.iocoder.yudao.module.train.controller.admin.appointment.vo.AppointmentRespVO;
import cn.iocoder.yudao.module.train.controller.admin.appointment.vo.AppointmentSaveReqVO;
import cn.iocoder.yudao.module.train.controller.admin.courseclasses.vo.VipClassesSaveReqVO;
import cn.iocoder.yudao.module.train.controller.admin.coursestudentregistform.vo.CourseStudentRegistFormPageReqVO;
import cn.iocoder.yudao.module.train.controller.admin.coursestudentregistform.vo.CourseStudentRegistFormSaveReqVO;
import cn.iocoder.yudao.module.train.dal.dataobject.appointment.AppointmentDO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.AppointmentDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.CourseStudentRegistFormDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.courseclasses.CourseClassesDO;
import cn.iocoder.yudao.module.train.dal.dataobject.courseclasses.VipClassesDO;
import cn.iocoder.yudao.module.train.dal.dataobject.coursecommodity.CourseCommodityDO;
import cn.iocoder.yudao.module.train.dal.dataobject.coursestudentregistform.CourseStudentRegistFormDO;
import cn.iocoder.yudao.module.train.service.appointment.AppointmentService;
import cn.iocoder.yudao.module.train.service.courseclasses.CourseClassesService;
import cn.iocoder.yudao.module.train.service.courseclasses.VipClassesService;
import cn.iocoder.yudao.module.train.service.coursecommodity.CourseCommodityService;
import cn.iocoder.yudao.module.train.service.coursestudentregistform.CourseStudentRegistFormService;
import cn.iocoder.yudao.module.train.service.scanrecord.ScanRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "小程序接口 - 预约管理")
@RestController
@RequestMapping("/wechat-api/appointment")
public class WechatAppointmentController {

    @Resource
    private AppointmentService appointmentService;

    @Resource
    private CourseCommodityService courseCommodityService;

    @Resource
    private ScanRecordService scanRecordService;

    @Resource
    private VipClassesService vipClassesService;

    @Resource
    private CourseStudentRegistFormService courseStudentRegistFormService;

    @GetMapping("/list")
    @Operation(summary = "学生预约列表")
    @ApiAccessLog(operateType = GET)
    public CommonResult<List<AppointmentRespVO>> listAppointments(AppointmentPageReqVO pageReqVO) {
        return success(BeanUtils.toBean(appointmentService.getAppointmentList(pageReqVO), AppointmentRespVO.class));
    }

    @PostMapping("/appointment-student")
    @Operation(summary = "学生预约")
    @ApiAccessLog(operateType = CREATE)
    public CommonResult<Boolean> appointmentStudent(AppointmentSaveReqVO reqVO) {
        VipClassesDO vipClassesDO = vipClassesService.getVipClassesByCommodityId(reqVO.getCommodityId());
        if (vipClassesDO == null) {
            VipClassesSaveReqVO saveReqVO = new VipClassesSaveReqVO();
            saveReqVO.setCommodityId(reqVO.getCommodityId());
            Long classesId = vipClassesService.createVipClasses(saveReqVO);
            CourseStudentRegistFormSaveReqVO saveReqVO2 = new CourseStudentRegistFormSaveReqVO();
            saveReqVO2.setClassesId(classesId);
            saveReqVO2.setStudentId(reqVO.getStudentId());
            Integer duration = courseCommodityService.getCourseCommodity(reqVO.getCommodityId()).getTeachingAllTime();
            saveReqVO2.setDuration(duration);
            saveReqVO2.setRestDuration(duration);
            courseStudentRegistFormService.createCourseStudentRegistForm(saveReqVO2);
            appointmentService.createAppointment(reqVO);
            return success(true, "预约成功");
        }
        List<CourseStudentRegistFormDTO> registList = courseStudentRegistFormService.getCourseStudentRegistFormListByClassesId(vipClassesDO.getId());
        for (CourseStudentRegistFormDTO registForm : registList) {
            if (registForm.getStudentId().equals(reqVO.getStudentId())) {
                appointmentService.createAppointment(reqVO);
                return success(true, "预约成功");
            }
        }
        CourseStudentRegistFormSaveReqVO saveReqVO2 = new CourseStudentRegistFormSaveReqVO();
        saveReqVO2.setClassesId(vipClassesDO.getId());
        saveReqVO2.setStudentId(reqVO.getStudentId());
        Integer duration = courseCommodityService.getCourseCommodity(reqVO.getCommodityId()).getTeachingAllTime();
        saveReqVO2.setDuration(duration);
        saveReqVO2.setRestDuration(duration);
        courseStudentRegistFormService.createCourseStudentRegistForm(saveReqVO2);
        appointmentService.createAppointment(reqVO);
        return success(true, "预约成功");
    }

    @PostMapping("/appointment-cancel")
    @Operation(summary = "学生取消预约")
    @ApiAccessLog(operateType = UPDATE)
    public CommonResult<Boolean> appointmentCancel(Integer id) {
        AppointmentSaveReqVO reqVO = new AppointmentSaveReqVO();
        reqVO.setId(id);
        reqVO.setAppointmentStatus(3);
        appointmentService.updateAppointment(reqVO);
        return success(true, "操作成功");
    }

    @PostMapping("/appointment-signin")
    @Operation(summary = "预约课程签到")
    @ApiAccessLog(operateType = CREATE)
    public CommonResult<Boolean> appointmentSignin(Integer id) {
        AppointmentDO appointmentDO = appointmentService.getAppointment(id);
        if (appointmentDO.getAppointmentStatus() != 1) {
            return error(1000_19_1, "预约非通过状态，不允许签到");
        }
        appointmentDO.setAppointmentStatus(4);
        VipClassesDO vipClassesDO = vipClassesService.getVipClassesByCommodityId(appointmentDO.getCommodityId());
        List<CourseStudentRegistFormDTO> list = courseStudentRegistFormService.getCourseStudentRegistFormListByClassesId(vipClassesDO.getId());
        for (CourseStudentRegistFormDTO form : list) {
            if (form.getStudentId().equals(appointmentDO.getStudentId())) {
                CourseStudentRegistFormDO validDO = courseStudentRegistFormService.getCourseStudentRegistForm(form.getId());
                CourseStudentRegistFormSaveReqVO validReqVO = BeanUtil.copyProperties(validDO, CourseStudentRegistFormSaveReqVO.class);
                CourseCommodityDO commodityDO = courseCommodityService.getCourseCommodity(appointmentDO.getCommodityId());
                validReqVO.setRestDuration(Math.max(validReqVO.getRestDuration() - commodityDO.getTeachingTime(), 0));
                courseStudentRegistFormService.updateCourseStudentRegistForm(validReqVO);
            }
        }
        return success(true, "签到成功");
    }
}
